package com.yan.demo03;

import com.yan.Server;
import jdk.nashorn.internal.scripts.JD;
import sun.misc.ProxyGenerator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author yanheng
 * @data 2020/1/19 11:01
 */
public class Demo {

    public static void saveProxyClass(Class clazz, String proxyName){
        byte[] bytes = ProxyGenerator.generateProxyClass(proxyName, clazz.getInterfaces());
//        String path = clazz.getResource(".").getPath();
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("F:/" + proxyName + ".class");
            fos.write(bytes);//保存到磁盘
            fos.flush();
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
            }catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        JdkProxyServer server = new JdkProxyServer();
        JDKProxyFactory factory = new JDKProxyFactory();
        Server proxyInstance = (Server)factory.createProxyInstance(server);
//        saveProxyClass(server.getClass(), "ProxyJdkProxyServer");
        proxyInstance.downloadResource();
    }
}
